home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SAMPLES / ACCUM.C next >
Encoding:
C/C++ Source or Header  |  1998-07-26  |  3.3 KB  |  156 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <GL/glut.h>
  28.  
  29.  
  30. GLenum doubleBuffer;
  31. GLint thing1, thing2;
  32.  
  33.  
  34. static void Init(void)
  35. {
  36.  
  37.     glClearColor(0.0, 0.0, 0.0, 0.0);
  38.     glClearAccum(0.0, 0.0, 0.0, 0.0);
  39.  
  40.     thing1 = glGenLists(1);
  41.     glNewList(thing1, GL_COMPILE);
  42.     glColor3f(1.0, 0.0, 0.0);
  43.     glRectf(-1.0, -1.0, 1.0, 0.0);
  44.     glEndList();
  45.  
  46.     thing2 = glGenLists(1);
  47.     glNewList(thing2, GL_COMPILE);
  48.     glColor3f(0.0, 1.0, 0.0);
  49.     glRectf(0.0, -1.0, 1.0, 1.0);
  50.     glEndList();
  51. }
  52.  
  53. static void Reshape(int width, int height)
  54. {
  55.  
  56.     glViewport(0, 0, (GLint)width, (GLint)height);
  57.  
  58.     glMatrixMode(GL_PROJECTION);
  59.     glLoadIdentity();
  60.     glMatrixMode(GL_MODELVIEW);
  61.     glLoadIdentity();
  62. }
  63.  
  64. static void Key(unsigned char key, int x, int y)
  65. {
  66.  
  67.     switch (key) {
  68.       case 27:
  69.     exit(1);
  70.       case '1':
  71.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  72.     break;
  73.       case '2':
  74.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  75.     break;
  76.       default:
  77.     return;
  78.     }
  79.  
  80.     glutPostRedisplay();
  81. }
  82.  
  83. static void Draw(void)
  84. {
  85.  
  86.     glPushMatrix();
  87.  
  88.     glScalef(0.8, 0.8, 1.0);
  89.  
  90.     glClear(GL_COLOR_BUFFER_BIT);
  91.     glCallList(thing1);
  92.     glAccum(GL_LOAD, 0.5);
  93.  
  94.     glClear(GL_COLOR_BUFFER_BIT);
  95.     glCallList(thing2);
  96.     glAccum(GL_ACCUM, 0.5);
  97.  
  98.     glAccum(GL_RETURN, 1.0);
  99.  
  100.     glPopMatrix();
  101.  
  102.     glFlush();
  103.  
  104.     if (doubleBuffer) {
  105.     glutSwapBuffers();
  106.     }
  107. }
  108.  
  109. static GLenum Args(int argc, char **argv)
  110. {
  111.     GLint i;
  112.  
  113.     doubleBuffer = GL_FALSE;
  114.  
  115.     for (i = 1; i < argc; i++) {
  116.     if (strcmp(argv[i], "-sb") == 0) {
  117.         doubleBuffer = GL_FALSE;
  118.     } else if (strcmp(argv[i], "-db") == 0) {
  119.         doubleBuffer = GL_TRUE;
  120.     } else {
  121.         printf("%s (Bad option).\n", argv[i]);
  122.         return GL_FALSE;
  123.     }
  124.     }
  125.     return GL_TRUE;
  126. }
  127.  
  128. void main(int argc, char **argv)
  129. {
  130.     GLenum type;
  131.  
  132.     glutInit(&argc, argv);
  133.  
  134.     if (Args(argc, argv) == GL_FALSE) {
  135.     exit(1);
  136.     }
  137.  
  138.     glutInitWindowPosition(0, 0);
  139.     glutInitWindowSize( 300, 300);
  140.  
  141.     type = GLUT_RGB | GLUT_ACCUM;
  142.     type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  143.     glutInitDisplayMode(type);
  144.  
  145.     if (glutCreateWindow("Accum Test") == GL_FALSE) {
  146.     exit(1);
  147.     }
  148.  
  149.     Init();
  150.  
  151.     glutReshapeFunc(Reshape);
  152.     glutKeyboardFunc(Key);
  153.     glutDisplayFunc(Draw);
  154.     glutMainLoop();
  155. }
  156.